﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mmoss.orm
{
    public partial class User 
    {

    }
    public static class ExtensionUser
    {
        public static User ValidateLogin(this Table<User> table
        , string username
        , string password
            )
        {
            return (from objs in table
                    where objs.userName.Trim().ToLower()==username.Trim().ToLower()
                    &&
                    objs.userPassword==password
                    select objs 
                    ).FirstOrDefault();
        }
        public static User ValidateLogin(this Table<User> table
        , string username
        , string password
            ,string l_perm)
        {
            User l_user = (from objs in table
                    where objs.userName.Trim().ToLower()==username.Trim().ToLower()
                    &&
                    objs.userPassword==password
                    select objs 
                    ).FirstOrDefault();
            if (l_user!=null && (l_user.UserPermissionsXrefs.Where(p=>
                p.UserPermission.permissionShortDesc == l_perm).Count() > 0))
            {
                return l_user;
            }
            else
            {
                return null;
            }
        }
        public static bool ValidatePerm(this User l_user, UserPermission l_perm)
        {
            //find out if user has a particular permission
            return (l_user.UserPermissionsXrefs.Select(p => p.UserPermission == l_perm) != null);
        }
        public static bool ValidatePerm(this User l_user, string l_permShort)
        {
            return (l_user.UserPermissionsXrefs.Where(p => p.UserPermission.permissionShortDesc == l_permShort).Count() > 0);
        }
        public static bool ValidatePerm(this User l_user, int l_permid)
        {
            //find out if user has a particular permission
            return (l_user.UserPermissionsXrefs.Select(p => p.UserPermissionID==l_permid) != null);
        }
    }

}
